package scm.rawMaterialPurchasing.managedbean;

import crm.salesOrderProcessing.entity.ProductEntity;
import java.io.IOException;
import java.io.Serializable;

import java.util.ArrayList;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import scm.rawMaterialPurchasing.entity.BOMLineItemEntity;
import scm.rawMaterialPurchasing.session.BOMSessionBean;

@ManagedBean
@ViewScoped
public class BomManageManagedBean implements Serializable {

    @EJB
    BOMSessionBean bomSessionBean;
    private ProductEntity selectedProduct;
    private List<BOMLineItemEntity> bomLineItemList, finalList;
    private BOMLineItemEntity bomLineItem;
    private double quantity;
    private String selectedRawMaterialName;

    public BomManageManagedBean() {

        bomSessionBean = new BOMSessionBean();
        bomLineItemList = new ArrayList<BOMLineItemEntity>();
        finalList = new ArrayList<BOMLineItemEntity>();
    }

    public void iniView(PhaseEvent event) {
        if (selectedProduct == null) {
            selectedProduct = ((ProductEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedProduct"));
            bomLineItemList = selectedProduct.getBillOfMaterials().getBomLineItems();
        }       
    }

    public void deleteLineItem(BOMLineItemEntity bomLineItem) {
        for (int i = 0; i < bomLineItemList.size(); i++) {
            
            
            if (bomLineItemList.get(i).getRawMaterial().getRawMaterialName().equals(bomLineItem.getRawMaterial().getRawMaterialName())) {
                 bomLineItemList.remove(i);
                break;
            }
        }
    }

    public void addLineItem(ActionEvent event) {
        bomLineItem = new BOMLineItemEntity();
        boolean flag = false;
        for (int i = 0; i < bomLineItemList.size(); i++) {
            if (bomLineItemList.get(i).getRawMaterial().getRawMaterialName().equals(selectedRawMaterialName)) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Product has already been added.", ""));
                flag = true;
                break;
            }
        }
        if (flag == false) {
            bomLineItem.setRawMaterial(bomSessionBean.getRawMaterialByName(selectedRawMaterialName));
            bomLineItem.setQuantity(getQuantity());
            bomLineItem.setBillOfMaterials(selectedProduct.getBillOfMaterials());

            bomLineItemList.add(bomLineItem);
        }

    }

    public List<String> getAllRawMaterialNames() {
        return bomSessionBean.getAllRawMaterialNames();
    }

    public void updateBOM(ActionEvent event) throws IOException {


        for (int i = 0; i < bomLineItemList.size(); i++) {
            BOMLineItemEntity tempLineItem = new BOMLineItemEntity();
            tempLineItem.setBillOfMaterials(bomLineItemList.get(i).getBillOfMaterials());
            tempLineItem.setQuantity(bomLineItemList.get(i).getQuantity());
            tempLineItem.setRawMaterial(bomLineItemList.get(i).getRawMaterial());
            getFinalList().add(tempLineItem);

        }
//       for(int i=0;i<bomLineItemList.size();i++)
//       {if (bomLineItemList.get(i).getId()!=null) 
//           bomLineItemList.get(i).setId(null);
//           
//       }
        System.out.println("??????????????????"+selectedProduct.getBillOfMaterials().getId());
        bomSessionBean.remove(selectedProduct.getBillOfMaterials().getId());
          bomSessionBean.save(finalList,selectedProduct.getBillOfMaterials().getId());
      
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Updated Successful.", ""));    
    }

    public List<BOMLineItemEntity> getBomLineItemList() {
        return bomLineItemList;
    }

    public void setBomLineItemList(List<BOMLineItemEntity> bomLineItemList) {
        this.bomLineItemList = bomLineItemList;
    }

    public ProductEntity getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(ProductEntity selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }

    public BOMLineItemEntity getBomLineItem() {
        return bomLineItem;
    }

    public void setBomLineItem(BOMLineItemEntity bomLineItem) {
        this.bomLineItem = bomLineItem;
    }

    public String getSelectedRawMaterialName() {
        return selectedRawMaterialName;
    }

    public void setSelectedRawMaterialName(String selectedRawMaterialName) {
        this.selectedRawMaterialName = selectedRawMaterialName;
    }

    public List<BOMLineItemEntity> getFinalList() {
        return finalList;
    }

    public void setFinalList(List<BOMLineItemEntity> finalList) {
        this.finalList = finalList;
    }
}
